﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "StructureColumn.h"
#include "GbmpBuiltInParameterDefinitionsNew.h"
#include "GcmpBuiltInParameterDefinitions.h"
#include "GmIStructureFactory.h"
#include "ICalculator.h"
#include "ICalculatorCollection.h"
#include "ICopyStrategyReporter.h"
#include "IDocument.h"
#include "IElementParameters.h"
#include "IElementParentReporter.h"
#include "IInstance.h"
#include "IParameter.h"
#include "IParameterDefinitionLibrary.h"
#include "IRegenerator.h"
#include "IRegeneratorDataIdCreator.h"
#include "StructureColumnInput.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

IMPLEMENT_REGEN_FUNCTIONS(gcmp, StructureColumn, Volume)

DEFINE_STRUCTURE_INSTANCE_METHODS(StructureColumn);

DATA_DEFINE(StructureColumn)
{
    m_pOwnerElement = nullptr;
    SetVolume__(0);
    SetConcreteStrengthGrade__(ConcreteStrengthGradeType::CSG_C30);
}

OwnerPtr<StructureColumn> StructureColumn::Create(IInstance* pInstance, const StructureColumnInput* pInput)
{
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pInstance, L"invalid pInstance",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pInput, L"invalid pInput",L"GDMPLab",L"2024-03-30");

    OwnerPtr<StructureColumn> opColumn = NEW_AS_OWNER_PTR(StructureColumn);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opColumn, L"invalid opColumn",L"GDMPLab",L"2024-03-30");

    opColumn->SetOwnerElement(pInstance);
    opColumn->SetConcreteStrengthGrade__(pInput->ConcreteType);

    return opColumn;
}

double StructureColumn::GetHeight() const
{
    DBG_WARN_AND_RETURN_UNLESS(m_pOwnerElement, 0.0, L"m_pOwnerElement为空",L"GDMPLab",L"2024-03-30");
    const IElementParameters* pElementParameters = m_pOwnerElement->GetElementParameters();
    DBG_WARN_AND_RETURN_UNLESS(pElementParameters, 0.0, L"pElementParameters为空",L"GDMPLab",L"2024-03-30");

    double height = 0;
    bool hasParameter = pElementParameters->GetParameterValueAsDouble(PARAMETER_UID(ColumnHeightBuiltInParameter), height);
    DBG_WARN_AND_RETURN_UNLESS(hasParameter, 0.0, L"paramHeight参数无效",L"GDMPLab",L"2024-03-30");
    
    return height;
}

void StructureColumn::UpdateForWeakParentDeletion(const std::set<ElementId>& deletedElementIds)
{
    return;
}

void StructureColumn::ReportParents(IElementParentReporter& reporter) const
{
}

DECLARE_CALCULATOR_CREATOR(InstanceVolumeCalculator)
void StructureColumn::GetCalculators(ICalculatorCollection * calculators) const
{
    ADD_CALCULATOR(InstanceVolumeCalculator, GetDocument(), GetVolumeRdId());
}

void StructureColumn::ReportParameterDefinitions(std::vector<int>* pParamDefIds) const
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pParamDefIds != nullptr, L"pParamDefUids为空",L"GDMPLab",L"2024-03-30");
    pParamDefIds->push_back(PARAMETER_ID(VolumeBuiltInParameter));
    pParamDefIds->push_back(PARAMETER_ID(ConcreteStrengthGradeBuiltInParameter));
}

OwnerPtr<IParameter> StructureColumn::GetNativeParameter(int paramDefId) const
{
    OwnerPtr<IParameter> param;
    ParameterAttributes pa = {false, false, true};
    if (paramDefId == PARAMETER_ID(VolumeBuiltInParameter))
    {
        //param = NEW_AS_OWNER_PTR(Parameter, GetDocument(), ParameterStorageType::Double, false, false,
            //true, PARAMETER_UID(VolumeBuiltInParameter), ParameterProcessType::GeneralOutput);
        param = IParameter::CreateParameter(GetDocument(), ParameterStorageType::Double, pa,
            PARAMETER_UID(VolumeBuiltInParameter), ParameterProcessType::GeneralOutput);
        param->SetValueAsDouble(GetVolume__());
        return TransferOwnership(param);
    }
    else if (paramDefId == PARAMETER_ID(ConcreteStrengthGradeBuiltInParameter))
    {
        //param = NEW_AS_OWNER_PTR(Parameter, GetDocument(), ParameterStorageType::Int, false, false,
            //true, PARAMETER_UID(ConcreteStrengthGradeBuiltInParameter), ParameterProcessType::GeneralInput);
        param = IParameter::CreateParameter(GetDocument(), ParameterStorageType::Int, pa,
            PARAMETER_UID(ConcreteStrengthGradeBuiltInParameter), ParameterProcessType::GeneralInput);
        param->SetValueAsInt((int)GetConcreteStrengthGrade__());
        return TransferOwnership(param);
    }
    return nullptr;
}

bool StructureColumn::SetNativeParameter(const IParameter *param, std::wstring* errorMsg/* = nullptr*/)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(param, L"param is null.",L"GDMPLab",L"2024-03-30");

    if (param->GetParameterDefinitionId() == PARAMETER_ID(VolumeBuiltInParameter))
    {
        SetVolume(param->GetValueAsDouble());
        return true;
    }
    else if (param->GetParameterDefinitionId() == PARAMETER_ID(ConcreteStrengthGradeBuiltInParameter))
    {
        SetConcreteStrengthGrade((ConcreteStrengthGradeType)param->GetValueAsInt());
        return true;
    }
    return false;
}

bool StructureColumn::IsParameterModifiable(int paramDefId) const
{
    if (paramDefId == PARAMETER_ID(VolumeBuiltInParameter))
    {
        return false;
    }
    else if (paramDefId == PARAMETER_ID(ConcreteStrengthGradeBuiltInParameter))
    {
        return true;
    }
    return false;
}

void StructureColumn::Report(const ICopyContext & copyContext, ICopyStrategyReporter& reporter) const
{
    reporter.ReportToCopy(GetOwnerElementId());
}

IDocument* StructureColumn::GetDocument() const
{
    return m_pOwnerElement ? m_pOwnerElement->GetDocument() : nullptr;
}

ElementId StructureColumn::GetOwnerElementId() const
{
    return m_pOwnerElement ? m_pOwnerElement->GetElementId() : ElementId::InvalidID;
}

NdbObject* StructureColumn::GetTopOwnerObject() const
{
    if (!GetDocument())
        return nullptr;

    return quick_cast<NdbObject>(GetDocument()->GetElement(GetOwnerElementId()));
}

